﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.Utils;
using DevExpress.XtraEditors;
using FileManager_Client.BusinessLogic;
using FileManager_Client.BusinessLogic.FileManagerUserService;
using FileManager_Client.Utility;

namespace FileManager_Client.PresentationLayer.NewUi
{
    public partial class AddMyDepartmentPeopleForm : BaseForm
    {
        #region Form Constructor
        private UserService _userService;
        private static object lockWait = new object();
        private LoadingWaitForm _loadingWaitForm;
        private UserLoginResult userSession;
        public List<UserResult> ChoosePeople { get; set; } 
        public AddMyDepartmentPeopleForm()
        {
            InitializeComponent();
            _userService = new UserService();
            ChoosePeople = new List<UserResult>();
            userSession = CacheGlobal.GetCache(GlobalString.System_Config_String.UserSession) as UserLoginResult;
        }
        #endregion

        #region Button Confirm Event
        private void Btn_Confirm_Click(object sender, EventArgs e)
        {
            foreach (TreeNode node in tv_MyDepartment.Nodes)
            {
                if (!node.Checked) continue;
                ChoosePeople.Add(node.Tag as UserResult);
            }
        }
        #endregion

        #region Form Event
        private void AddMyDepartmentPeopleForm_Shown(object sender, EventArgs e)
        {
            tv_MyDepartment.Nodes.Clear();
            var searchCompany = new SearchUserByCompanyCodeArgs
            {
                SearchUserByCompanyEnumk__BackingField = SearchUserByCompanyEnum.Company,
                CodeCollectionk__BackingField = new[] { userSession.CompanyIdk__BackingField }
            };
            var backgroundWorker = new BackgroundWorker();
            backgroundWorker.DoWork += backgroundWorker_DoWork;
            backgroundWorker.RunWorkerAsync(searchCompany);
            lock (lockWait)
            {
                _loadingWaitForm = new LoadingWaitForm();
                _loadingWaitForm.SetCaption(GlobalString.System_Config_String.MbxInfoStr);
                _loadingWaitForm.SetDescription(GlobalString.System_Config_String.SearchUserStr);
                _loadingWaitForm.ShowDialog();
            }
        }

        void backgroundWorker_DoWork(object sender, DoWorkEventArgs e)
        {
            var result = _userService.SearchUserByCompanyCode(e.Argument as SearchUserByCompanyCodeArgs);
            Invoke((EventHandler)delegate
            {
                _loadingWaitForm.Close();
                foreach (var userResult in result.Resultk__BackingField)
                {
                    var node = new TreeNode
                    {
                        ImageIndex = 0,
                        Text = userResult.UserNamek__BackingField,
                        Tag = userResult
                    };
                    tv_MyDepartment.Nodes.Add(node);
                }
                tv_MyDepartment.Update();
            });
        }
        #endregion
    }
}